package com.ekuaitu.kuaitustaff.application;

import android.content.Context;
import android.text.TextUtils;

import com.ekuaitu.kuaitustaff.BuildConfig;
import com.ekuaitu.kuaitustaff.api.ApiService;
import com.ekuaitu.kuaitustaff.api.ApiStore;
import com.ekuaitu.kuaitustaff.api.UrlConstant;
import com.ekuaitu.kuaitustaff.bean.ConfigBean;
import com.ekuaitu.kuaitustaff.utils.Logger;
import com.ekuaitu.kuaitustaff.utils.SPKey;
import com.ekuaitu.kuaitustaff.utils.SPUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by michaelpi on 2018/6/11.
 */

public class GetAppConfig {
    private static GetAppConfig instance;
    private Context mContext;
    public boolean isUpdate = false;//apiConfig是否加载成功

    public interface UpdateDone {
        void onUpdateDone();
    }

    public static GetAppConfig getInstance(Context context) {
        if (instance == null) {
            instance = new GetAppConfig(context);
        }
        return instance;
    }

    private GetAppConfig(Context context) {
        mContext = context;
    }

    public void getConfig(UpdateDone updateDone) {
        if (BuildConfig.DEBUG) {
            switch (SPUtils.getInstance(mContext).getIntValue(SPKey.URLMODE)) {
                case 0://默认
                case 2://test
                case 3://pro
                    //0 2 3需要调用apiConfig
                    break;
                default://无需调用apiConfig，如果调用了直接回调告知成功
                    if (updateDone != null) {
                        updateDone.onUpdateDone();
                    }
                    return;
            }
        }


        new ApiStore().getConfigRetrofit().create(ApiService.class).appConfig(MyApplication.getInstance().getAppVersion(), "android").enqueue(new Callback<ConfigBean>() {
            @Override
            public void onResponse(Call<ConfigBean> call, Response<ConfigBean> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getData() != null) {
                    Logger.i("apiConfig", "get config ok");
                    setUrl(response.body().getData());
                    MyApplication.findCarVip = response.body().getData().getCommon().getFindCarVIP();
                    SPUtils.getInstance(mContext).writeObject(SPKey.CONFIG_BENA, response.body());
                } else {
                    Logger.i("apiConfig", "get config err");
                }
                if (updateDone != null) {
                    updateDone.onUpdateDone();
                }
            }

            @Override
            public void onFailure(Call<ConfigBean> call, Throwable t) {
                if (updateDone != null) {
                    updateDone.onUpdateDone();
                }
                Logger.i("apiConfig", "get config failure" + t.getMessage());
            }
        });
    }

    private void setUrl(ConfigBean.DataBean data) {

        if (data.getCommon() != null && data.getCommon().getServer_url() != null) {
            if (!TextUtils.isEmpty(data.getCommon().getServer_url().getApiServerNew())) {
                UrlConstant.Request.BASE_URL_NEW = data.getCommon().getServer_url().getApiServerNew() + "/";
            }
            if (data.getCommon().getH5_url() != null) {
                UrlConstant.Request.HOTLINE = data.getCommon().getH5_url().getCallservice();
                UrlConstant.Request.SETTING = data.getCommon().getH5_url().getSetpage();
                UrlConstant.Request.WORKLIST = data.getCommon().getH5_url().getWorklist();
                UrlConstant.Request.PARK_FEE = data.getCommon().getH5_url().getSubmitParkingFee();
                UrlConstant.Request.CAR_BREAK = data.getCommon().getH5_url().getCarBreakage();
                Logger.i("apiConfig设置了url1");
            }
        }
        isUpdate = true;//apiConfig已加载成功
    }
}
